/*
 * Date          Author     Note
 * 2015.04.29    wzwz       报警屏蔽
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhedit.h"
#include "mhlabel.h"
#include "alarm.h"
#include "com.h"
#include "info.h"
#include "utf8_gb2312.h"

static void OnKeyDown(HWND hWnd, uint key);
/*
 *
 */
static void CreateAlarmInterface(HWND hWnd)
{
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_FILE_INPUT_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);

    const char *sc[25] = {
            GetString(645), GetString(646), GetString(647), GetString(648),
            GetString(649), GetString(650), GetString(651), GetString(652),
            GetString(653), GetString(654), GetString(655), GetString(656),
            GetString(657), GetString(658), GetString(659), GetString(660),
            GetString(661), GetString(662), GetString(663), GetString(664),
            GetString(665), GetString(666), GetString(667), GetString(668),
            GetString(669),
    };
    int i = 0;
    union {
        ALARM_MASK mask;
        int all;
    }bit;
    bit.all = GetAlarmMask(g_knit_data);
    uchar alarm[25] = {0};
    alarm[0] = bit.mask.left_feeder;
    alarm[1] = bit.mask.right_feeder;
    alarm[2] = bit.mask.left_break;
    alarm[3] = bit.mask.right_break;
    alarm[4] = bit.mask.balcony;
    alarm[5] = bit.mask.knot;
    alarm[6] = bit.mask.rab;
    alarm[7] = bit.mask.roll;
    alarm[8] = bit.mask.storage;
    alarm[9] = bit.mask.has_roll;
    alarm[10] = bit.mask.front_probe;
    alarm[11] = bit.mask.back_probe;
    alarm[12] = bit.mask.left_drt;
    alarm[13] = bit.mask.right_drt;
    alarm[14] = bit.mask.scram;             //安全停车
    alarm[15] = bit.mask.ac110;             //交流接触器
    alarm[16] = bit.mask.back_safe_door;
    alarm[17] = bit.mask.right_back_safe_door;
    alarm[18] = bit.mask.front_safe_door;
    alarm[19] = bit.mask.left_rubber;         //橡胶断沙
    alarm[20] = bit.mask.board;
    alarm[21] = bit.mask.fender;                    //起底版保护盖
    alarm[22] = bit.mask.board_zero;
    alarm[23] = bit.mask.board_high;
    alarm[24] = bit.mask.drop_bad;
    for(i = 0; i < 25; ++i)
    {
        CreateWindow(MH_LABEL_NAME,
            sc[i],
            WS_CHILD | WS_VISIBLE | BS_LEFT,
            IDC_STATIC,
            20 + (i / 10) * 140,
            60 + (i % 10) * 30,
            98,
            20,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        CreateWindow(MH_EDIT_NAME,
            alarm[i] ? GetString(672) : GetString(671),
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + i,
            119 + (i / 10) * 140,
            60 + (i % 10) * 30,
            36,
            20,
            hWnd, alarm[i] ? MH_EDIT_TEXT_WHITE : 0);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{

    switch(nID)
    {
    case ID_ESC:
        {
            if(GetWindowAdditionalData(hWnd))
            {
                int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
                if(ret)
                {
                    printf("write alarm mask: %d\n", ret);
                    AfxInfoBox(hWnd, GetString(392), GetString(481));
                }
                else
                {
                    MH_FILE_SYNC();
                }
               // SendSysParam(g_knit_data);
               SendAlarmMask(g_knit_data);
            }
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 25)
    {
        OnKeyDown(hWnd, SCANCODE_ENTER);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        int mask = 0;
        switch(nID)
        {
        case IDC_EDIT1:
            mask = SwitchLeftFeederAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 1:
            mask = SwitchRightFeederAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 2:
            mask = SwitchLeftBreakAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 3:
            mask = SwitchRightBreakAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 4:
            mask = SwitchBalconyAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 5:
            mask = SwitchKnotAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 6:
            mask = SwitchRabAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 7:
            mask = SwitchRollAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 8:
            mask = SwitchStorageAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 9:
            mask = SwitchMacHasRoll(g_knit_data);
            break;
        case IDC_EDIT1 + 10:
            mask = SwitchFrontProbeAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 11:
            mask = SwitchBackProbeAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 12:
            mask = SwitchLeftDrtAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 13:
            mask = SwitchRightDrtAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 14:
            mask = SwitchScramAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 15:
            mask = SwitchAC110Alarm(g_knit_data);
            break;
        case IDC_EDIT1 + 16:
            mask = SwitchBackSafeDoorAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 17:
            mask = SwitchRightBackSafeDoorAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 18:
            mask = SwitchFrontSafeDoorAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 19:
            mask = SwitchLeftRubberAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 20:
            mask = SwitchBoardAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 21:
            mask = SwitchFenderAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 22:
            mask = SwitchBoardZeroAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 23:
            mask = SwitchBoardHighAlarm(g_knit_data);
            break;
        case IDC_EDIT1 + 24:
            mask = SwitchDropBadAlarm(g_knit_data);
            break;
        }
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 25)
        {
            HWND hTmp = GetDlgItem(hWnd, nID);
            SetWindowCaption(hTmp, mask ? GetString(672) : GetString(671));
            SetEditTextColor(hTmp, mask ? MH_EDIT_TEXT_WHITE : 0);
            InvalidateRect(hTmp, NULL, TRUE);
            SetWindowAdditionalData(hWnd, TRUE);
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 + 10 && nID < IDC_EDIT1 + 25)
        {
            nID -= 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 15)
        {
            nID += 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > IDC_EDIT1 && nID < IDC_EDIT1 + 25)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 24)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int AlarmWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateAlarmInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 150;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 150;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_FILE_INPUT]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateAlarmWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_FILE_INPUT_BMP) / 2;
    int y = (CY_SCREEN - CY_FILE_INPUT_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(670);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = AlarmWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_FILE_INPUT_BMP;
    CreateInfo.by = y + CY_FILE_INPUT_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
